{
数据结构 解题报告：
http://git.oschina.net/et0662/sznoi-Data_Structure
}

type 
	pnode=^node;
	node=record
		data:longint;
		next:pnode;
	end;
	stack=node;
	pstack=^stack;

procedure push(s:pstack; n:longint);
var
	p:pnode;
begin
	new(p);
	p^.data:=n;
	p^.next:=nil;
	if(s^.next<>nil)then
	begin
		p^.next:=s^.next;
	end;
	s^.next:=p;
end;

procedure pop(s:pstack);
var
	p:pnode;
begin
	if(s^.next<>nil)then
	begin
		p:=s^.next;
		s^.next:=p^.next;
		dispose(p);
	end;
end;

procedure a(s:pstack);
begin
	if(s^.next<>nil)then
	begin
		writeln(s^.next^.data);
	end
	else
		writeln('E');
end;

function initStack():pstack;
var
	p:pnode;
begin
	new(p);
	p^.next:=nil;
	initStack:=p;
end;

var
	c:char;
	s:pstack;
	t:longint;
	i,n:integer;
begin
	readln(n);
	s:=initStack();
	for i:=1 to n do
	begin
		read(c);
		case c of
			'P': begin readln(t); push(s,t); end;
			'O':begin pop(s); readln end;
			'A':begin a(s); readln end;
		end;
	end;
end.
